package br.unicamp.ic.lis.dbvapi.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.genericspatialdao.utils.ConstantsUtils;

import br.unicamp.ic.lis.dbvapi.exception.DBVException;

public class StampUtils {

	public static final String ROOT_STAMP = "0";
	public static final String FIRST_CHILD = "0.1";
	private static final String BAD_STAMP = "Bad stamp: ";

	private static Logger LOG = Logger.getLogger(StampUtils.class);

	public static String[] getStampChain(String stamp) {
		if (StringUtils.isBlank(stamp)) {
			return null;
		}
		return stamp.split("\\.");
	}

	public static List<String> getStampChainAsList(String stamp) {
		if (StringUtils.isBlank(stamp)) {
			return null;
		}
		String[] stampChain = stamp.split("\\.");
		List<String> stampChainList = new ArrayList<String>();
		for (int i = 0; i < stampChain.length; i++) {
			stampChainList.add(i, stampChain[i]);
		}

		return stampChainList;
	}

	public static String getParentStamp(String stamp) {
		if (StringUtils.isBlank(stamp)) {
			LOG.error(BAD_STAMP + stamp);
			throw new DBVException(BAD_STAMP + stamp);
		}
		if (StringUtils.equalsIgnoreCase(stamp, ROOT_STAMP)) {
			return null;
		} else {
			List<String> stampChainList = getStampChainAsList(stamp);
			int lastIndex = stampChainList.size() - 1;
			stampChainList.remove(lastIndex);			
			String result = buildStamp(stampChainList);
			LOG.debug("Parent stamp: " + result);

			return result;
		}
	}
	
	/*
	public static String getParentStamp(String stamp) {
		if (StringUtils.isBlank(stamp)) {
			LOG.error(BAD_STAMP + stamp);
			throw new DBVException(BAD_STAMP + stamp);
		}
		if (StringUtils.equalsIgnoreCase(stamp, ROOT_STAMP)) {
			return null;
		} else {
			List<String> stampChainList = getStampChainAsList(stamp);
			int lastIndex = stampChainList.size() - 1;

			while (stampChainList.size() >= 2
					&& StringUtils.equalsIgnoreCase(
							stampChainList.get(lastIndex), ROOT_STAMP)) {
				stampChainList.remove(lastIndex);
				lastIndex--;
			}

			Integer aux = Integer.valueOf(stampChainList.get(lastIndex));
			if (aux == null) {
				LOG.error(BAD_STAMP + stamp);
				throw new DBVException(BAD_STAMP + stamp);
			}
			stampChainList.set(lastIndex, String.valueOf(--aux));

			String result = buildStamp(stampChainList);
			LOG.debug("Parent stamp: " + result);

			return result;
		}
	}
*/
	public static String removeLastNumber(String stamp) {
		if (StringUtils.isBlank(stamp)) {
			return null;
		}
		List<String> stampChainList = getStampChainAsList(stamp);
		int lastIndex = stampChainList.size() - 1;
		stampChainList.remove(lastIndex);
		return buildStamp(stampChainList);
	}

	public static String buildStamp(List<String> stampChain) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < stampChain.size(); i++) {
			sb.append(stampChain.get(i));
			if (i != stampChain.size() - 1) {
				sb.append(ConstantsUtils.DOT);
			}
		}
		return sb.toString();
	}	
}
